package manage.commons.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 
 * Description：Annontation工具类
 * 
 * @since：2012-8-17 下午09:22:11
 * @author dongji
 * 
 */
public class AnnontationUtils {

	/**
	 * 取方法上的注解
	 * 
	 * @param <T>
	 * @param annotationClazz
	 * @param o
	 * @param methodName
	 * @return
	 */
	public static <T extends Annotation> T getFromMethed(
			Class<T> annotationClazz, Object o, String methodName) {
		if (o == null) {
			return null;
		}
		Class<? extends Object> objectClass = o.getClass();
		try {
			Method method = objectClass.getDeclaredMethod(methodName);
			return method.getAnnotation(annotationClazz);
		} catch (SecurityException e) {
			throw new RuntimeException(e);
		} catch (NoSuchMethodException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 先取方法上的注解，取不到在取类上面的
	 * 
	 * @param <T>
	 * @param annotationClazz
	 * @param o
	 * @param methodName
	 * @return
	 */
	public static <T extends Annotation> T getFromMethedOrType(
			Class<T> annotationClazz, Object o, String methodName) {
		T annotation = getFromMethed(annotationClazz, o, methodName);
		if (annotation != null) {
			return annotation;
		}
		Class<? extends Object> objectClass = o.getClass();
		return objectClass.getAnnotation(annotationClazz);
	}
}
